/* CreateTablesFigs.do

This Stata do file produces the tables and figures for

Conor M. Dowling, David Doherty,  Seth J. Hill, Alan S. Gerber, Gregory A. Huber
"The Voting Experience and Beliefs about Ballot Secrecy."
PLOS ONE 2019.

The do file requires the Stata add ons outreg2 and estout, which can be found by
net search outreg2 and
net search estout.
*/

use "DowlingEtAlVotingExperienceBeliefsAboutBallotSecrecy.dta", clear

*** 
tab other_info if other_info!="NONE"
***
**EXPERIENCE RESPONSE CATEGORY INDICATORS
foreach i in 19 20 23 92 17 21 22 18 {
tab yal0`i', gen(dum_yal0`i'_)
if `i'==92{
rename dum_yal0`i'_2 dum_yal0`i'_3
gen dum_yal0`i'_2=0
}
}

** Officials access
tab dum_yal092_3  [aw=weight] if timing_3!=1 & lvote_3!=1 
*** TOP LEVEL FIGURE
foreach i in 19 20 23 17 21 22 18 {
forvalues a=3(-1)1{
sum dum_yal0`i'_`a' [aw=weight]
if `a'==3{
matrix means_`i'=r(mean)
}
else{
matrix means_`i'=means_`i',r(mean)
}
}
matrix means_`i'=means_`i',r(N)
if `i'==19{
matrix means=means_`i'
}
else{
matrix means=means\means_`i'
}
}
matrix output_full=means

*** TOP LEVEL FIGURE--VALIDATED VOTERS ONLY
foreach i in 19 20 23 17 21 22 18 {
forvalues a=3(-1)1{
sum dum_yal0`i'_`a' if vote_gen==1 [aw=weight]
if `a'==3{
matrix means_`i'=r(mean)
}
else{
matrix means_`i'=means_`i',r(mean)
}
}
matrix means_`i'=means_`i',r(N)
if `i'==19{
matrix means=means_`i'
}
else{
matrix means=means\means_`i'
}
}
matrix output_full_valid=means



*** OUTPUT MEANS ***
svmat output_full, names(full)
svmat output_full_valid, names(valid)

outsheet full* valid* using top_level.csv,comma replace

drop full*


*** NEW RESPONSE CATEGORIES [_2 = just "yes"; _1 = "yes" and "dk"]
foreach i in 19 20 23 21 22 18 {
gen cat_yal0`i'_2=dum_yal0`i'_3
gen cat_yal0`i'_1=dum_yal0`i'_3+dum_yal0`i'_2
}
gen cat_yal017_2=dum_yal017_1
gen cat_yal017_1=dum_yal017_1+dum_yal017_2

gen cat_yal092_2=dum_yal092_3
gen cat_yal092_1=dum_yal092_3

*** HYPOTHETICAL v. IN-PERSON VARIABLES
foreach i in 19 20 23 92 17 21 22 18 {
local hyp=`i'+20

if `i'!=92{
tab yal0`hyp', gen(dum_yal0`hyp'_)
}

forvalues a=1/3{
gen merge_dum_yal0`i'_`a'=dum_yal0`i'_`a' 
if `i'!=92{
replace merge_dum_yal0`i'_`a'=dum_yal0`hyp'_`a' if lvote_3==1
}
}
}

*** COLLAPSE RESPONSE CATEGORIES for NOW-COMBINED Hypothetical v. In-Person
foreach i in 19 20 23 21 22 18 {
gen merge_cat_yal0`i'_2=merge_dum_yal0`i'_3
gen merge_cat_yal0`i'_1=merge_dum_yal0`i'_3+merge_dum_yal0`i'_2
}
gen merge_cat_yal017_2=merge_dum_yal017_1
gen merge_cat_yal017_1=merge_dum_yal017_1+merge_dum_yal017_2

gen merge_cat_yal092_2=merge_dum_yal092_3
gen merge_cat_yal092_1=merge_dum_yal092_3

*** TOP LEVEL SECRECY
recode yal110 (1=0) (2=100) (*=.), gen(kept_secret)
recode yal111 (1=100) (2/5=0) (*=.), gen(find_out)

**OUTPUT REGRESSIONS
*top level secrecy
qui reg kept_secret electronic timing_2 timing_3 lvote_2 lvote_3 [aw=weight], r
outreg2 electronic timing_2 timing_3 lvote_2 lvote_3 using "pred_regressions_toplevel.out", se bracket rdec(3) replace
qui reg kept_secret electronic timing_2 timing_3 lvote_2 lvote_3 r_black r_hispanic r_otherrace gender age age2 educ income incomemis state* [aw=weight], r
outreg2 electronic timing_2 timing_3 lvote_2 lvote_3 r_black r_hispanic r_otherrace gender age age2 educ income incomemis using "pred_regressions_toplevel.out", se bracket rdec(3) append
qui reg find_out electronic timing_2 timing_3 lvote_2 lvote_3 [aw=weight], r
outreg2 electronic timing_2 timing_3 lvote_2 lvote_3 using "pred_regressions_toplevel.out", se bracket rdec(3) append
qui reg find_out electronic timing_2 timing_3 lvote_2 lvote_3 r_black r_hispanic r_otherrace gender age age2 educ income incomemis state* [aw=weight], r
outreg2 electronic timing_2 timing_3 lvote_2 lvote_3 r_black r_hispanic r_otherrace gender age age2 educ income incomemis using "pred_regressions_toplevel.out", se bracket rdec(3) append
qui reg merge_cat_yal092_1 electronic timing_2 timing_3 lvote_2 lvote_3 [aw=weight], r
outreg2 electronic timing_2 timing_3 lvote_2 lvote_3 using "pred_regressions_toplevel.out", se bracket rdec(3) append
qui reg merge_cat_yal092_1 electronic timing_2 timing_3 lvote_2 lvote_3 r_black r_hispanic r_otherrace gender age age2 educ income incomemis state* [aw=weight], r
outreg2 electronic timing_2 timing_3 lvote_2 lvote_3 r_black r_hispanic r_otherrace gender age age2 educ income incomemis using "pred_regressions_toplevel.out", se bracket rdec(3) append

*formal protections
foreach i in 19 20 23  {
qui reg merge_cat_yal0`i'_1 electronic timing_2 timing_3 lvote_2 lvote_3 [aw=weight], r
if `i'==19{
outreg2 electronic timing_2 timing_3 lvote_2 lvote_3 using "pred_regressions_formal.out", se bracket rdec(3) replace
}
else{
outreg2 electronic timing_2 timing_3 lvote_2 lvote_3 using "pred_regressions_formal.out", se bracket rdec(3) append
}
qui reg merge_cat_yal0`i'_1 electronic timing_2 timing_3 lvote_2 lvote_3 r_black r_hispanic r_otherrace gender age age2 educ income incomemis state* [aw=weight], r
outreg2 electronic timing_2 timing_3 lvote_2 lvote_3 r_black r_hispanic r_otherrace gender age age2 educ income incomemis using "pred_regressions_formal.out", se bracket rdec(3) append
}

*other voters
foreach i in 17 21 22 18 {
qui reg merge_cat_yal0`i'_1 electronic timing_2 timing_3 lvote_2 lvote_3 [aw=weight], r
if `i'==17{
outreg2 electronic timing_2 timing_3 lvote_2 lvote_3 using "pred_regressions_othvoters.out", se bracket rdec(3) replace
}
else{
outreg2 electronic timing_2 timing_3 lvote_2 lvote_3 using "pred_regressions_othvoters.out", se bracket rdec(3) append
}
qui reg merge_cat_yal0`i'_1 electronic timing_2 timing_3 lvote_2 lvote_3 r_black r_hispanic r_otherrace gender age age2 educ income incomemis state* [aw=weight], r
outreg2 electronic timing_2 timing_3 lvote_2 lvote_3 r_black r_hispanic r_otherrace gender age age2 educ income incomemis using "pred_regressions_othvoters.out", se bracket rdec(3) append
}


***FIGURES
**to show %s
foreach i in 92 19 20 23 17 21 22 18 {
replace merge_cat_yal0`i'_1 =merge_cat_yal0`i'_1 *100
}

qui reg kept_secret electronic timing_2 timing_3 lvote_2 lvote_3 [aw=weight], r
eststo kept_secret
qui reg kept_secret electronic timing_2 timing_3 lvote_2 lvote_3 r_black r_hispanic r_otherrace gender age age2 educ income incomemis state* [aw=weight], r
eststo kept_secret_ctrl

qui reg find_out electronic timing_2 timing_3 lvote_2 lvote_3 [aw=weight], r
eststo find_out
qui reg find_out electronic timing_2 timing_3 lvote_2 lvote_3 r_black r_hispanic r_otherrace gender age age2 educ income incomemis state* [aw=weight], r
eststo find_out_ctrl

qui reg merge_cat_yal092_1 electronic timing_2 timing_3 lvote_2 lvote_3 [aw=weight], r
eststo secr_92
qui reg merge_cat_yal092_1 electronic timing_2 timing_3 lvote_2 lvote_3 r_black r_hispanic r_otherrace gender age age2 educ income incomemis state* [aw=weight], r
eststo secr_92_ctrl

foreach i in 19 20 23  {
qui reg merge_cat_yal0`i'_1 electronic timing_2 timing_3 lvote_2 lvote_3 [aw=weight], r
eststo secr_`i'
qui reg merge_cat_yal0`i'_1 electronic timing_2 timing_3 lvote_2 lvote_3 r_black r_hispanic r_otherrace gender age age2 educ income incomemis state* [aw=weight], r
eststo secr_`i'_ctrl
}

foreach i in 17 21 22 18 {
qui reg merge_cat_yal0`i'_1 electronic timing_2 timing_3 lvote_2 lvote_3 [aw=weight], r
eststo secr_`i'
qui reg merge_cat_yal0`i'_1 electronic timing_2 timing_3 lvote_2 lvote_3 r_black r_hispanic r_otherrace gender age age2 educ income incomemis state* [aw=weight], r
eststo secr_`i'_ctrl
}
	
coefplot (kept_secret,  msymbol(O) connect(none) mcolor(black) ciopts(lcolor(black) lwidth(thin) msize(vtiny)) label(Choices not secret)) (kept_secret_ctrl,  msymbol(O) connect(none) mcolor(gs8) ciopts(lcolor(gs8) lwidth(thin) msize(vtiny) lpattern(dash)) label(Choices not secret)) (kept_secret,  msymbol(none) connect(none) ciopts(lcolor(none))) (find_out,  msymbol(T) connect(none) mcolor(black) ciopts(lcolor(black) lwidth(thin) msize(vtiny)) label(Not at all difficult to find out)) (find_out_ctrl,  msymbol(T) connect(none) mcolor(gs8) ciopts(lcolor(gs8) lwidth(thin) msize(vtiny) lpattern(dash)) label(Not at All Difficult to Find Out)) (kept_secret,  msymbol(none) connect(none) ciopts(lcolor(none))) (secr_92,  msymbol(S) connect(none) mcolor(black) ciopts(lcolor(black) lwidth(thin) msize(vtiny)) label(Elected officials access)) (secr_92_ctrl,  msymbol(S) connect(none) mcolor(gs8) ciopts(lcolor(gs8) lwidth(thin) msize(vtiny) lpattern(dash)) label(Elected Officials Access))  , keep(electronic_ballot timing_2 timing_3 lvote_2 lvote_3 )  xlabel(0 10 20 30 40 50) level(95) xtitle("Difference in Percent with Secrecy Doubts",  size(medsmall)) graphregion(fcolor(white) lcolor(none) ilcolor(none) color(white) lwidth(large)) xline(0, lc(black) lw(thin) ) xscale(lw(none)) legend(colfirst order(2 8 14) col(2) span region(style(none))) note("Whiskers indicate 95% confidence intervals. Gray markers are from models that control for respondent" "demographics. Reference categories: Paper ballot; Election Day voter; Voted in 2010 General election.", span) title("Figure 2. Top-level secrecy perceptions", span size(medium))
graph export "general_secrecy.tif", as(tif) width(4000) replace

coefplot (secr_19,  msymbol(O) connect(none) mcolor(black) ciopts(lcolor(black) lwidth(thin) msize(vtiny)) label(Name on ballot)) (secr_19_ctrl,  msymbol(O) connect(none) mcolor(gs8) ciopts(lcolor(gs8) lwidth(thin) msize(vtiny) lpattern(dash)) label(Name on Ballot)) (kept_secret,  msymbol(none) connect(none) ciopts(lcolor(none))) (secr_20,  msymbol(T) connect(none) mcolor(black) ciopts(lcolor(black) lwidth(thin) msize(vtiny)) label(Identifier on ballot)) (secr_20_ctrl,  msymbol(T) connect(none) mcolor(gs8) ciopts(lcolor(gs8) lwidth(thin) msize(vtiny) lpattern(dash)) label(Name on Ballot)) (kept_secret,  msymbol(none) connect(none) ciopts(lcolor(none))) (secr_23,  msymbol(S) connect(none)  mcolor(black) ciopts(lcolor(black) lwidth(thin) msize(vtiny)) label(Worker look)) (secr_23_ctrl,  msymbol(S) connect(none) mcolor(gs8) ciopts(lcolor(gs8) lwidth(thin) msize(vtiny) lpattern(dash)) label(Name on Ballot)), keep(lvote_2 lvote_3 electronic_ballot timing_2 )   xlabel(0 10 20 30 40 50) level(95) xtitle("Difference in Percent with Secrecy Doubts",  size(medsmall)) graphregion(fcolor(white) lcolor(none) ilcolor(none) color(white) lwidth(large)) xline(0, lc(black) lw(thin) ) xscale(lw(none)) legend(colfirst order(2 8 14) col(3) span region(style(none)))  note("Whiskers indicate 95% confidence intervals. Gray markers are from models that control for respondent" "demographics. Reference categories: Paper ballot; Election Day voter; Voted in 2010 General election.", span) title("Figure 3. Formal secrecy perceptions", span size(medium))
graph export "formal_process.tif", as(tif) width(4000) replace

coefplot (secr_17,  msymbol(O) connect(none) mcolor(black) ciopts(lcolor(black) lwidth(thin) msize(vtiny)) label(Privacy screen)) (secr_17_ctrl,  msymbol(O) connect(none) mcolor(gs8) ciopts(lcolor(gs8) lwidth(thin) msize(vtiny) lpattern(dash)) label(Privacy Screen))(kept_secret,  msymbol(none) connect(none) ciopts(lcolor(none))) (secr_18,  msymbol(D) connect(none) mcolor(black) msize(small) ciopts(lcolor(black) lwidth(thin) msize(vtiny)) label(Asked in line)) (secr_18_ctrl,  msymbol(D) msize(small) connect(none) mcolor(gs8) ciopts(lcolor(gs8) lwidth(thin) msize(vtiny) lpattern(dash)) label(Asked in line)) (kept_secret,  msymbol(none) connect(none) ciopts(lcolor(none))) (secr_21,  msymbol(T) connect(none) mcolor(black) ciopts(lcolor(black) lwidth(thin) msize(vtiny)) label(I could see others)) (secr_21_ctrl,  msymbol(T) connect(none) mcolor(gs8) ciopts(lcolor(gs8) lwidth(thin) msize(vtiny) lpattern(dash)) label(I could see others)) (kept_secret,  msymbol(none) connect(none) ciopts(lcolor(none))) (secr_22,  msymbol(S) connect(none) mcolor(black) ciopts(lcolor(black) lwidth(thin) msize(vtiny)) label(Others could see me)) (secr_22_ctrl,  msymbol(S) connect(none) mcolor(gs8) ciopts(lcolor(gs8) lwidth(thin) msize(vtiny) lpattern(dash)) label(Others could see me)) , keep(lvote_2 lvote_3 electronic_ballot timing_2 )   xlabel(0 10 20 30 40 50) level(95) xtitle("Difference in Percent with Secrecy Doubts",  size(medsmall)) graphregion(fcolor(white) lcolor(none) ilcolor(none) color(white) lwidth(large)) xline(0, lc(black) lw(thin) ) xscale(lw(none)) legend(colfirst order(2 8 14 20) col(2) span region(style(none)))  note("Whiskers indicate 95% confidence intervals. Gray markers are from models that control for respondent" "demographics. Reference categories: Paper ballot; Election Day voter; Voted in 2010 General election.", span) title("Figure 4. Social secrecy perceptions", span size(medium))
graph export "other_voters.tif", as(tif) width(4000) replace


**summary statistics
outsum electronic timing_2 timing_3 lvote_2 lvote_3 r_black r_hispanic r_otherrace gender age age2 educ income incomemis using "sumstats", replace bracket 
